package com.myk.game.heroscuffle.heroes;

import com.myk.game.heroscuffle.game.Attacked;
import com.myk.game.heroscuffle.game.Element;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.signs.CountSign_ChanDou;
import com.myk.game.heroscuffle.skills.Skill_TongXin;
import com.myk.game.heroscuffle.skills.Skill_ZhanJue;
import com.myk.game.heroscuffle.utils.RdmPropUtil;
import com.myk.game.heroscuffle.utils.WeighterTask;

public class Hero_MuBai extends Hero {

    public Hero_MuBai() {
        super("慕白",
                "被他攻击后的英雄攻击力降低一回合。", Element.DARK,
                1200, 160, 54,
                new Skill_ZhanJue(), new Skill_TongXin());
    }

    @Override
    public void onAcceptPlayerAttackAction(Attacked preAttacked) {
        if (preAttacked.getTarget() instanceof Hero_ShuiQing && game.pool.getAliveCount() > 2) {
            Hero attacked = game.pool.getAnyoneAliveHero(this, preAttacked.getTarget()); //另外选一个目标
            this.doAttackActionStart(new Attacked(attacked, "自作主张攻击"));
        }
        else {
            Hero attacked = preAttacked.getTarget(); //系统默认选中的目标
            this.doAttackActionStart(new Attacked(attacked, "遵从指定攻击"));
        }
    }

    @Override
    public void onAutoSelectAction() {
        Hero target = super.autoSelectAttacked().getTarget();
        if (target instanceof Hero_ShuiQing) { //若目标是水清
            faceImportant(target);
        }
        else {
            faceAnther(target);
        }
    }

    //面对重要的人时的判断
    private void faceImportant(Hero target) {
        Skill_TongXin tongxin = (Skill_TongXin) getUsableSkill(Skill_TongXin.class);
        if (tongxin != null) { //尽量使用同心技能
            tongxin.setIntentTarget(target);
            useSkill(tongxin);
        }
        else {
            if (game.pool.getAliveCount() == 2) { //如果只剩下水清，且同心技能没有了
                doAttackActionStart(new Attacked(target, "攻击"));//万不得已只能攻击水清
            }
            else {
                Hero anther = game.pool.getAnyoneAliveHero(this, target);
                faceAnther(anther);
            }
        }
    }

    //面对其它人时的判断
    private void faceAnther(Hero anther) {
        Skill_ZhanJue juezhan = (Skill_ZhanJue) getUsableSkill(Skill_ZhanJue.class);
        Skill_TongXin tongxin = (Skill_TongXin) getUsableSkill(Skill_TongXin.class);
        Runnable doAttack = () -> doAttackActionStart(new Attacked(anther, "攻击"));
        Runnable useJueZhan = () -> {
            if (juezhan != null) {
                juezhan.setIntentTarget(anther);
                useSkill(juezhan);
            }
            else {
                doAttack.run();
            }
        };
        Runnable useTongXin = () -> {
            if (tongxin != null) {
                tongxin.setIntentTarget(anther);
                useSkill(tongxin);
            }
            else {
                doAttack.run();
            }
        };

        boolean highHp = anther.hp > anther.HP / 2;
        if (anther.hp < 200 && juezhan != null) { //逮到机会必用决斩
            useJueZhan.run();
        }
        else {
            if (anther == state.getAliveEnemy()) {
                if (highHp) {
                    RdmPropUtil.lotteryExecute(new WeighterTask(2, useTongXin), new WeighterTask(1, doAttack));
                }
                else {
                    RdmPropUtil.lotteryExecute(new WeighterTask(2, useJueZhan), new WeighterTask(1, useTongXin), new WeighterTask(3, doAttack));
                }
            }
            else {
                if (highHp) {
                    RdmPropUtil.lotteryExecute(new WeighterTask(1, useTongXin), new WeighterTask(2, doAttack));
                }
                else {
                    RdmPropUtil.lotteryExecute(new WeighterTask(2, useJueZhan), new WeighterTask(1, useTongXin), new WeighterTask(3, doAttack));
                }
            }
        }
    }

    @Override
    public void onAfterDoAttack(Hero attacked, int damage, boolean isToDeath) {
        if (!isToDeath) {
            if (!attacked.hasSign(CountSign_ChanDou.class)) {
                attacked.pasteSign(new CountSign_ChanDou());
                attacked.event.onAfterAction.addRunBack(eventData -> {
                    attacked.removeSign(CountSign_ChanDou.class);
                    eventData.removeRunBackFromListener();
                });
            }
        }
    }
}
